home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / network / lattice / portlib.lzh / PORTLIB / INITGRP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-09  |  950 b   |  52 lines

  1. /*
  2.  * BSD initgroups() emulation. Written by Kay Roemer.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <grp.h>
  9. #include <osbind.h>
  10.  
  11. #ifndef Psetgroups
  12. #define Psetgroups(a,b)    trap_1_wwl (0x148, (short)a, (long)b)
  13. #define Pgetgroups(a,b)    trap_1_wwl (0x147, (short)a, (long)b)
  14. #endif
  15.  
  16. #undef  NGROUPS_MAX
  17. #define NGROUPS_MAX    8
  18.  
  19. extern int errno;
  20.  
  21. int
  22. initgroups (user, group)
  23.     char *user;
  24.     gid_t group;
  25. {
  26.     static short groups[NGROUPS_MAX];
  27.     short ngroups = 0;
  28.     struct group *gre;
  29.     char **names;
  30.     long r;
  31.  
  32.     setgrent ();
  33.     while ((gre = getgrent ()) != NULL) {
  34.         for (names = gre->gr_mem; *names; ++names) {
  35.             if (strcmp (*names, user))
  36.                 continue;
  37.             if (ngroups >= NGROUPS_MAX)
  38.                 continue;
  39.             groups[ngroups++] = gre->gr_gid;
  40.         }
  41.     }
  42.     endgrent ();
  43.     if (ngroups > 0) {
  44.         r = Psetgroups (ngroups, groups);
  45.         if (r < 0) {
  46.             errno = -r;
  47.             return -1;
  48.         }
  49.     }
  50.     return 0;
  51. }
  52.